package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Commodity;
import cn.wolfcode.luowowo.article.domain.UserCommodity;
import cn.wolfcode.luowowo.article.domain.UserGold;
import cn.wolfcode.luowowo.article.query.CommodityQuery;
import cn.wolfcode.luowowo.article.query.ItempShopCommentQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("itemshop")
public class ItemShopController {

    @Reference
    private IUserGoldService userGoldService;

    @Reference
    private ICommodityService commodityService;

    @Reference
    private IUserCommodityService userCommodityService;

    @Reference
    private IShopService shopService;

    @Reference
    private IItempShopService itempShopService;

    @Reference
    private IItempShopCommentService itempShopCommentService;


    @RequireLogin
    @RequestMapping("")
    public String list(Model model, @UserParam UserInfo userInfo, HttpServletResponse response){

//        Integer count = 1;
//
//        CookieUtil.addCookie(response,"coin",count.toString(),3);

        Long uid = userInfo.getId();

        //当前用户拥有的金币
        UserGold userGold = userGoldService.selectByUserId(uid);
        model.addAttribute("userGold",userGold);

        //所有的商品
        List<Commodity> commodities = commodityService.listAll();
        model.addAttribute("commodities",commodities);

        //所有已经拥有的商品
        List<UserCommodity> userCds = userCommodityService.selectByUserId(uid);
        model.addAttribute("userCds",userCds);

        return "itemshop/list";
    }

    @RequestMapping("query")
    public String query(Model model,@UserParam UserInfo userInfo,Long queryType, CommodityQuery query){

        PageInfo pageInfo = shopService.query(userInfo,queryType,query);

        model.addAttribute("pageInfo",pageInfo);

        if (pageInfo.getList().get(0).getClass().equals(Commodity.class)){
            return "itemshop/itemTempl";
        }

        return "itemshop/myItemTempl";

    }

    @RequestMapping("buy")
    @ResponseBody
    public Object buy(Model model,@UserParam UserInfo userInfo,Long id){

        Long uid = userInfo.getId();

        int result = shopService.buy(uid,id);

        AjaxResult ajaxResult = new AjaxResult();

        ajaxResult.setSuccess(true);

        if (result == UserGold.NO_MONEY ){
            ajaxResult.setMsg("账户余额不足,请充值");
            ajaxResult.setSuccess(false);
        }

        if (result == Commodity.NO_NUMBER ){
            ajaxResult.setMsg("商品数量不足");
            ajaxResult.setSuccess(false);
        }

        return ajaxResult ;
    }

    @RequestMapping("comment")
    public String comment(Model model, @UserParam UserInfo userInfo, Long toId, String content, ItempShopCommentQuery qo){

        //要是有内容就保存
        if (content.length() > 0){
            itempShopCommentService.save(userInfo,content,toId);
        }

        //获取一个分页对象
        PageInfo page = itempShopCommentService.query(qo);

        model.addAttribute("page",page);

        return "itemshop/commentTempl";
    }

    @RequestMapping("/coin")
    @ResponseBody
    public Object coin(Model model, @UserParam UserInfo userInfo, HttpServletRequest request){

//        Cookie[] cookies = request.getCookies();
//
//        Integer count = 0;
//
//        for (Cookie cookie : cookies) {
//            if (cookie.getName().equals("count")){
//                count = Integer.parseInt(cookie.getValue());
//            }
//        }


        Long uid = userInfo.getId();

        int addCoin = 25 + (int) (Math.random() * (1 - 3));

            userGoldService.addCoin(uid,addCoin);

            String coinString = String.valueOf(addCoin);

            String msg = "恭喜你获得" + coinString + "金币";

            AjaxResult success = AjaxResult.SUCCESS;
            success.setMsg(msg);

            return success;


    }

}
